package com.smec.moblie.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by sunxinwei on 15/12/29.
 */
public class Util {

    /**
     * 返回当前程序版本名
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }

    /***
     * @param mntSchDate
     * @param mntTimeRangeCode
     * @return 保养详情，保养时间转化
     */
    public static String generateSchTime(String mntSchDate, String mntTimeRangeCode) {
        StringBuffer schDateTimeBuf = new StringBuffer();
        // 计划日期
        String spStr[] = mntSchDate.split(" ");
        schDateTimeBuf.append(spStr[0] + " ");
        // 计划时段
        if ("AFTERNOON".equals(mntTimeRangeCode)) {
            schDateTimeBuf.append("下午");
        } else if ("MORNING".equals(mntTimeRangeCode)) {
            schDateTimeBuf.append("上午");
        } else if ("EVENING".equals(mntTimeRangeCode)) {
            schDateTimeBuf.append("晚上");
        } else {
            schDateTimeBuf.append("未知时段");
        }

        return schDateTimeBuf.toString();
    }

    /***
     * 分秒格式时间获取
     *
     * @return
     */
    public static String getSystemDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String date = sdf.format(new Date());
        return date;
    }

    /***
     * 当前时间获取
     *
     * @return
     */
    public static String getSystemDateNow() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String date = sdf.format(new Date());
        return date;
    }

    /***
     * 当前时间获取
     *
     * @return
     */
    public static String getSystemDateDD() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String date = sdf.format(new Date());
        return date;
    }


    /**
     * 把日期转换成分秒格式
     *
     * @param date
     * @return
     */
    public static String changeToHHmm(String date) {
        String HHmm = "";
        if (null == date || date.equals("")) {
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date newDate = new Date();
            try {
                newDate = sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd HH:mm");
            HHmm = sdf2.format(newDate);
        }
        return HHmm;
    }

    /**
     * 把日期yyyy/MM/dd HH:mm:ss转换成dd/MM/yyyy HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String changeToheng(String date) {
        String HHmm = "";
        if (null == date || date.equals("")) {
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date newDate = new Date();
            try {
                newDate = sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            HHmm = sdf2.format(newDate);
        }
        return HHmm;
    }

    /***
     * 时间格式转化为 月／日
     *
     * @param mntSchDate
     * @return
     */
    public static String generateSchTime(String mntSchDate) {
        String schDateTimeBuf = "";
        String spStr0[] = mntSchDate.split(" ");
        String spStr[] = spStr0[0].split("/");
        schDateTimeBuf = spStr[1] + "/" + spStr[2];
        return schDateTimeBuf;
    }

    /***
     * 时间格式转化为 年 /月／日
     *
     * @param maintenancefastrepairhitime
     * @return
     */
    public static String getmaintenancefastrepairhiTime(String maintenancefastrepairhitime) {
        String spStr0[] = maintenancefastrepairhitime.split(" ");
        return spStr0[0];
    }

    /***
     * 根据TimeCode转化时间段
     *
     * @param mntTimeRangeCode
     * @return
     */
    public static String generateSchTimeCode(String mntTimeRangeCode) {
        String schDateTimeBuf = "";
        if ("AFTERNOON".equals(mntTimeRangeCode)) {
            schDateTimeBuf = "下午";
        } else if ("MORNING".equals(mntTimeRangeCode)) {
            schDateTimeBuf = "上午";
        } else if ("EVENING".equals(mntTimeRangeCode)) {
            schDateTimeBuf = "晚上";
        } else {
            schDateTimeBuf = "未知";
        }

        return schDateTimeBuf.toString();
    }

    /***
     * 空字符串转化
     *
     * @param str
     * @return
     */
    public static String conversionStr(String str) {
        if (null == str || "".equals(str)) {
            return "";
        } else {
            return str;
        }
    }

    /**
     * 读取SD卡中文本文件
     *
     * @param fileName
     * @return
     */
    public static String readAssetsFile(String fileName, Context context) {
        String str = "";
        InputStream abpath = context.getClass().getResourceAsStream("/assets/" + fileName);
        try {
            str = new String(InputStreamToByte(abpath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            str = "";
        } catch (IOException e) {
            e.printStackTrace();
            str = "";
        }
        return str;
    }

    private static byte[] InputStreamToByte(InputStream is) throws IOException {
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bytestream.write(ch);
        }
        byte imgdata[] = bytestream.toByteArray();
        bytestream.close();
        return imgdata;
    }


    /***
     * 获取当前时间 年月
     * * example:201601
     *
     * @return (int) 当前年月
     */
    private static String getRequestDatabe() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");//设置日期格式
        return df.format(new Date());
    }

    /***
     * @param amount 1 后一个月 －1前一个月
     * @return 年月 example 201601
     * @throws Exception
     */
    public static String getFrontBackStrDate(int amount) throws Exception {
        String strDate = "";
        strDate = getRequestDatabe();

        if (null == strDate) {
            return null;
        }
        try {
            DateFormat fmt = new SimpleDateFormat("yyyyMM");
            Calendar c = Calendar.getInstance();
            c.setTime(fmt.parse(strDate));
            c.add(Calendar.MONTH, amount);
            return fmt.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 大写转小写
     * @param str
     * @return
     */
    public static String exChangeAtoa(String str){
        StringBuffer sb = new StringBuffer();
        if(str!=null){
            for(int i=0;i<str.length();i++){
                char c = str.charAt(i);
                if(Character.isUpperCase(c)){
                    sb.append(Character.toLowerCase(c));
                }else{
                    sb.append(c);
                }
            }
        }

        return sb.toString();
    }
}
